import 'package:flutter/material.dart';

/*点击事件widget*/
// ignore: must_be_immutable
class TapWidget extends StatelessWidget {
  final Widget? child;
  final GestureTapCallback? onTap;
  final GestureTapDownCallback? onTapDown;
  final GestureTapUpCallback? onTapUp;
  final GestureTapCancelCallback? onTapCancel;
  final GestureTapCallback? onDoubleTap;
  final GestureLongPressCallback? onLongPress;
  final HitTestBehavior? behavior;
  final bool touchLimit; //是否点击限制, 默认为true
  final int touchLimitSecond; //点击限制时长,默认1s一次

  TapWidget({
    Key? key,
    this.child,
    this.onTap,
    this.onTapCancel,
    this.onTapUp,
    this.onTapDown,
    this.onDoubleTap,
    this.onLongPress,
    this.behavior,
    this.touchLimit = true,
    this.touchLimitSecond = 1,
  }) : super(key: key);

  bool _isTap = false;

  void _onTapAction() {
    //1秒点一次
    if (_isTap == true && touchLimit == true) {
      return;
    }
    _isTap = true;
    onTap?.call();
    Future.delayed(Duration(seconds: touchLimitSecond)).then((_) {
      _isTap = false;
    });
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onTap != null ? _onTapAction : null,
      onTapDown: onTapDown,
      onTapUp: onTapUp,
      onDoubleTap: onDoubleTap,
      onLongPress: onLongPress,
      onTapCancel: onTapCancel,
      behavior: behavior ?? HitTestBehavior.opaque,
      child: child,
    );
  }
}
